package com.example.musicplayer.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.musicplayer.R;
import com.example.musicplayer.musicClass.MusicInfo;
import com.example.musicplayer.service.MusicService;

import java.util.ArrayList;
import java.util.List;

public class MusicItemAdapter extends BaseAdapter {
    private static final int MODE_START = 1;
    private static final int MODE_END = 5;

    public static final int MODE_SEARCH = 1;
    public static final int MODE_NORMAL = 2;
    public static final int MODE_FAVORITE = 3;
    public static final int MODE_MUSIC_MENU_NORMAL = 4;
    public static final int MODE_MUSIC_MENU_LISTEN_INFO = 5;

    private final Context mContext;
    private List<MusicInfo> mMusicList;
    private final MusicService.MusicBinder mMusicBinder;
    private int mMode;
    private OnLocateMusicListener mOnLocateMusicListener;
    private OnMusicDeleteListener mOnMusicDeleteListener;
    private OnMoreFeaturesListener mOnMoreFeaturesListener;

    public interface OnLocateMusicListener {
        void onLocateMusic(MusicInfo musicInfo);
    }

    public interface OnMusicDeleteListener {
        void onMusicDelete(MusicInfo musicInfo);
    }

    public interface OnMoreFeaturesListener {
        void onExpandMoreFeatures(MusicInfo musicInfo);

        void onRequestMusicVideo(MusicInfo musicInfo);
    }

    public MusicItemAdapter(Context context, List<MusicInfo> musicList, MusicService.MusicBinder musicBinder, int mode) {
        mContext = context;
        mMusicList = musicList;
        mMusicBinder = musicBinder;
        mMode = mode;
    }

    public void setMode(int mode) {
        if (mode >= MODE_START && mode <= MODE_END) {
            mMode = mode;
        }
    }

    public List<MusicInfo> getMusicList() {
        return mMusicList;
    }

    public void setMusicList(List<MusicInfo> musicList) {
        mMusicList = musicList;
        notifyDataSetChanged();
    }

    public void setOnLocateMusicListener(OnLocateMusicListener listener) {
        mOnLocateMusicListener = listener;
    }

    public void setOnMusicDeleteListener(OnMusicDeleteListener listener) {
        mOnMusicDeleteListener = listener;
    }

    public void setOnMoreFeaturesListener(OnMoreFeaturesListener listener) {
        mOnMoreFeaturesListener = listener;
    }

    @Override
    public int getCount() {
        return mMusicList == null ? 0 : mMusicList.size();
    }

    @Override
    public Object getItem(int position) {
        return mMusicList == null ? null : mMusicList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.music_item, null);
            ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    (int) mContext.getResources().getDimension(R.dimen.music_item_view_height));
            convertView.setLayoutParams(lp);
            viewHolder.mPlayingIndicator = convertView.findViewById(R.id.playing_indicator);
            viewHolder.mIndicateText = convertView.findViewById(R.id.indicate_text);
            viewHolder.mFirstText = convertView.findViewById(R.id.first_text);
            viewHolder.mSecondText = convertView.findViewById(R.id.second_text);
            viewHolder.mListenInfo = convertView.findViewById(R.id.music_listen_info);
            viewHolder.mVideo = convertView.findViewById(R.id.music_video);
            viewHolder.mFeature = convertView.findViewById(R.id.more_feature);
            viewHolder.mDelete = convertView.findViewById(R.id.delete);
            viewHolder.mAddToList = convertView.findViewById(R.id.add_to_list);
            viewHolder.mLocate = convertView.findViewById(R.id.locate);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        updateDisplayStyle(viewHolder);
        //set music info
        MusicInfo musicInfo = mMusicList.get(position);
        if (musicInfo != null) {
            String secondText = musicInfo.getArtist() + " - " + musicInfo.getAlbum();
            viewHolder.mFirstText.setText(musicInfo.getTitle());
            viewHolder.mSecondText.setText(secondText);
            //listen counts if show
            if (viewHolder.mListenInfo.getVisibility() == View.VISIBLE) {
                int counts = musicInfo.getListenCounts();
                ((TextView) convertView.findViewById(R.id.music_listen_counts)).setText(String.valueOf(counts));
            }
            //check if is playing
            if (mMusicBinder != null && mMusicBinder.getId() == musicInfo.getId()) {
                viewHolder.mPlayingIndicator.setVisibility(View.VISIBLE);
                int textColor = mContext.getResources().getColor(R.color.spring_green);
                viewHolder.mIndicateText.setTextColor(textColor);
                viewHolder.mFirstText.setTextColor(textColor);
                viewHolder.mSecondText.setTextColor(textColor);
            } else {
                viewHolder.mPlayingIndicator.setVisibility(View.GONE);
                viewHolder.mIndicateText.setTextColor(mContext.getResources().getColor(R.color.music_item_first_text));
                viewHolder.mFirstText.setTextColor(mContext.getResources().getColor(R.color.music_item_first_text));
                viewHolder.mSecondText.setTextColor(mContext.getResources().getColor(R.color.music_item_second_text));
            }
        }
        //check if show indicator number
        if (viewHolder.mIndicateText.getVisibility() == View.VISIBLE) {
            viewHolder.mIndicateText.setText(String.valueOf(position + 1));
        }
        //add listener
        viewHolder.mAddToList.setOnClickListener(v -> {
            if (mMusicBinder != null) {
                List<MusicInfo> list = new ArrayList<>();
                list.add(mMusicList.get(position));
                mMusicBinder.addToPlayList(list);
            }
        });
        viewHolder.mLocate.setOnClickListener(v -> {
            if (mOnLocateMusicListener != null) {
                mOnLocateMusicListener.onLocateMusic(mMusicList.get(position));
            }
        });
        viewHolder.mDelete.setOnClickListener(v -> {
            if (mOnMusicDeleteListener != null) {
                mOnMusicDeleteListener.onMusicDelete(mMusicList.get(position));
            }
        });
        viewHolder.mVideo.setOnClickListener(v -> {
            if (mOnMoreFeaturesListener != null) {
                mOnMoreFeaturesListener.onRequestMusicVideo(mMusicList.get(position));
            }
        });
        viewHolder.mFeature.setOnClickListener(v -> {
            if (mOnMoreFeaturesListener != null) {
                mOnMoreFeaturesListener.onExpandMoreFeatures(mMusicList.get(position));
            }
        });
        return convertView;
    }

    private void updateDisplayStyle(ViewHolder viewHolder) {
        int video = View.GONE;
        int feature = View.GONE;
        int delete = View.GONE;
        int indicateText = View.GONE;
        int listenInfo = View.GONE;
        int addToPlayList = View.VISIBLE;
        int locate = View.GONE;
        //set recording to mode
        switch (mMode) {
            case MODE_SEARCH:
                feature = View.VISIBLE;
                locate = View.VISIBLE;
                break;
            case MODE_FAVORITE:
                video = View.VISIBLE;
                delete = View.VISIBLE;
                break;
            case MODE_MUSIC_MENU_NORMAL:
                video = View.VISIBLE;
                feature = View.VISIBLE;
                indicateText = View.VISIBLE;
                break;
            case MODE_MUSIC_MENU_LISTEN_INFO:
                feature = View.VISIBLE;
                indicateText = View.VISIBLE;
                listenInfo = View.VISIBLE;
                break;
            case MODE_NORMAL:
            default:
                video = View.VISIBLE;
                feature = View.VISIBLE;
                break;
        }
        viewHolder.mVideo.setVisibility(video);
        viewHolder.mFeature.setVisibility(feature);
        viewHolder.mDelete.setVisibility(delete);
        viewHolder.mIndicateText.setVisibility(indicateText);
        viewHolder.mListenInfo.setVisibility(listenInfo);
        viewHolder.mAddToList.setVisibility(addToPlayList);
        viewHolder.mLocate.setVisibility(locate);
    }

    static class ViewHolder {
        View mPlayingIndicator;
        TextView mIndicateText;
        TextView mFirstText;
        TextView mSecondText;
        View mListenInfo;
        View mVideo;
        View mFeature;
        View mDelete;
        View mAddToList;
        View mLocate;
    }
}